﻿using Daemon.Model;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Daemon.Data.Infrastructure.Auth
{
    public abstract class BaseAuthInfo : IAuthInfo
    {
        public int UserId { get; set; }

        public string UserName { get; set; }

        public BaseAuthUser UserEntity { get; set; }

        public JObject AuthorizationTree { get; set; }

        public bool IsAdmin { get; set; }

        public bool IsAuthorizedFor(string permission, string operation)
        {
            if (IsAdmin)
            {
                return true;
            }

            if (AuthorizationTree["securedItems"][permission] == null)
            {
                return false;
            }

            var permissions = ((JArray)AuthorizationTree["securedItems"][permission]).Select(x => (string)x).ToList();
            return permissions.Contains(operation);
        }

    }
}
